package com.xyz.rok.tools.controller;

import com.xyz.rok.tools.model.Propose;
import com.xyz.rok.tools.model.Result;
import com.xyz.rok.tools.model.request.ProposeRequest;
import com.xyz.rok.tools.service.ProposeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Date;

/**
 * @author ZhangYiXin
 * @version 1.0
 * @date 2021/6/18 10:44
 **/
@Slf4j
@RestController
@RequestMapping("/api/message")
public class MessageController {

    @Autowired
    private ProposeService proposeService;
    @Value("${weixin.token}")
    private String token;

    @PostMapping("/addPropose")
    public Result addPropose(@RequestBody ProposeRequest proposeRequest) {
        Propose propose = new Propose();
        BeanUtils.copyProperties(proposeRequest, propose);
        propose.setCreateTime(new Date());
        proposeService.insertSelective(propose);
        return Result.success();
    }

    @RequestMapping(value = "/push", consumes = "application/json")
    public String push(HttpServletRequest request) {
        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");
        //将token、timestamp、nonce三个参数进行字典排序
        String[] arr = new String[]{token, timestamp, nonce};
        Arrays.sort(arr);

        StringBuilder content = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            content.append(arr[i]);
        }

        MessageDigest md = null;
        String tmpStr = null;

        try {
            md = MessageDigest.getInstance("SHA-1");
            // 将三个参数字符串拼接成一个字符串进行sha1加密
            byte[] digest = md.digest(content.toString().getBytes());
            tmpStr = byteToStr(digest);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        if (tmpStr.equals(signature.toUpperCase())) {
            log.info("接收到消息");
        } else {

        }
        return tmpStr != null ? tmpStr.equals(signature.toUpperCase()) ? echostr : "" : "";
    }

    /**
     * 将字节数组转换为十六进制字符串
     *
     * @param byteArray
     * @return
     */
    private String byteToStr(byte[] byteArray) {
        String strDigest = "";
        for (int i = 0; i < byteArray.length; i++) {
            strDigest += byteToHexStr(byteArray[i]);
        }
        return strDigest;
    }

    /**
     * 将字节转换为十六进制字符串
     *
     * @param mByte
     * @return
     */
    private String byteToHexStr(byte mByte) {
        char[] Digit = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        char[] tempArr = new char[2];
        tempArr[0] = Digit[(mByte >>> 4) & 0X0F];
        tempArr[1] = Digit[mByte & 0X0F];

        String s = new String(tempArr);
        return s;
    }
}
